package com.jf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf.blog.bean.Blog;
import com.jf.blog.bean.BlogTag;
import com.jf.blog.bean.Comment;
import com.jf.blog.form.blog.BlogForm;
import com.jf.blog.form.blog.BlogSearchForm;
import com.jf.blog.mapper.BlogMapper;
import com.jf.blog.service.BlogService;
import com.jf.blog.service.BlogTagService;
import com.jf.blog.service.CommentService;
import com.jf.blog.util.LoginUtil;
import com.jf.blog.util.MyConstants;
import com.jf.blog.util.ResultJson;
import com.jf.blog.util.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 博客表 服务实现类
 * </p>
 *
 * @author JF
 * @since 2025-06-20
 */
@Service
@Transactional
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
    @Autowired
    private BlogTagService blogTagService;
    @Autowired
    private CommentService commentService;

    @Override
    public void saveOrUpdate(BlogForm form) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(form, blog);
        this.saveOrUpdate(blog);

        /*删除当前博客关联的tag表数据*/
        blogTagService.remove(new QueryWrapper<BlogTag>().eq("blog_id", blog.getBlogId()));
        /*封装批量添加的标签集合*/
//        List<BlogTag> blogList = new ArrayList<>();
//        for (int tagId : form.getTagIds()) {
//            BlogTag bt = new BlogTag();
//            bt.setBlogId(blog.getBlogId());
//            bt.setTagId(tagId);
//            blogList.add(bt);
//        }

        List<BlogTag> blogTagList = form.getTagIds().stream().map(item -> new BlogTag(blog.getBlogId(), item)).collect(Collectors.toList());
        // 批量添加博客的标签
        blogTagService.saveBatch(blogTagList);

    }

    @Override
    public ResultJson getPage(int page, int limit, BlogSearchForm form) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(form, blog);
        if (LoginUtil.getLoginUser().getRoleId() == MyConstants.ROLE_VIP) {
            blog.setCreateUser(LoginUtil.getLoginUser().getUserinfoId());
        }

        Date startTime = null;
        Date endTime = null;
        if (form.getApplyTimeRange() != null) {
            startTime = form.getApplyTimeRange()[0];
            endTime = form.getApplyTimeRange()[1];
        }

        IPage<Blog> iPage = this.page(new Page<>(page, limit),
                new QueryWrapper<Blog>()
                        .like(StringUtils.isNotEmpty(blog.getTitle()), "title", blog.getTitle())
                        .eq(blog.getCategoryId() != null, "category_id", blog.getCategoryId())
                        .eq(blog.getHasComment() != null, "has_comment", blog.getHasComment())
                        .eq(blog.getBlogStatus() != null, "blog_status", blog.getBlogStatus())
                        .eq(blog.getStatus() != null, "status", blog.getStatus())
                        .eq(blog.getCreateUser() != null, "create_user", blog.getCreateUser())
                        .between(form.getApplyTimeRange() != null, "apply_time", startTime, endTime)
                        .orderByDesc("update_time", "create_time")
        );
        return ResultJson.success(iPage);
    }

    @Override
    public void delete(List<Integer> blogIds) {
        this.removeByIds(blogIds);
        blogTagService.remove(new QueryWrapper<BlogTag>().in("blog_id", blogIds));
        commentService.remove(new QueryWrapper<Comment>().in("blog_id", blogIds));
    }

    @Override
    public void updateBlogStatus(int blogId) {
        Blog blog = this.getById(blogId);
        if (blog == null) {
            throw new BusinessException("博客编号异常！");
        }
        int blogStatus = blog.getBlogStatus() == 1 ? 2 : 1;
        blog = new Blog();
        blog.setBlogStatus(blogStatus);
        blog.setBlogId(blogId);

        this.updateById(blog);
    }

    @Override
    public void updateStatus(int blogId) {
        Blog blog = this.getById(blogId);
        if (blog == null) {
            throw new BusinessException("博客编号异常！");
        }
        int status = blog.getStatus() == 1 ? 2 : 1;
        blog = new Blog();
        blog.setStatus(status);
        blog.setBlogId(blogId);

        this.updateById(blog);
    }
}
